﻿#include "controldevice.h"

#include <QDebug>

#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>

#include "./mixdevice.h"
#include "./deviceconversion.h"
#include "./devicestatus.h"
#include "./protocolconvert.h"
#include "../commons/configpin.h"
#include "../commons/pinsinfors.h"
#include "../commons/regionalpins.h"
#include "../commons/cleaninfors.h"
#include "../commons/blowairpins.h"

#include "../commons/configfile.h"
#include "../mydatas/handldatabase.h"
#include "../tasks/timetasks.h"
#include "../commons/commonblowair.h"

#include "../updates/updateprogrems.h"
#include "../commons/levelpin.h"
#include "./fridgedevice.h"

#include "../logs/printlogs.h"


ControlDevice * ControlDevice::m_controlDevice = NULL;
ControlDevice::ControlDevice(QObject *parent) : QObject(parent)
  , m_isClean(false)
  , m_isCleanStops(false)
  , m_isCleanPause(false)
  , m_isCleanPiPles(false)
  , m_isOut(false)
  , m_mcuTimer(NULL)
  , m_testTimer(NULL)
  , m_mcuUpdateTimes(NULL)
  , m_outTimer (NULL)
  , m_cleanTimerout(NULL)
  , m_mcuCleanTimer(NULL)
  , m_cleanLens(0)
{
    if(!getStartCompany()){
        setStartCompany(true);
    }

    QString docParhFile =  QApplication::applicationDirPath();

    QString docParhFile1 =docParhFile+"/";
    QDir isDir1;

    //设置顶端配置路径
    if (isDir1.exists(docParhFile1+"files/" )) {
        docParhFile1+= "files/";
        QDir isDir2;
        QString sns = ConfigFile::gestance()->getSns();
        if (isDir2.exists(docParhFile1+sns+"/" )) {
            docParhFile1+= sns+"/";

            QDir dir(docParhFile1);
            if(dir.exists())
            {
                dir.setFilter(QDir::Files | QDir::NoSymLinks);
                QFileInfoList list = dir.entryInfoList();

                int file_count = list.count();
                if(file_count > 0)
                {
                    QStringList string_list;
                    for(int i=0; i<file_count; i++)
                    {
                        QFileInfo file_info = list.at(i);
                        QString suffix = file_info.suffix();
                        QString fileStr1 = file_info.absoluteFilePath();
                        QString fileNames = file_info.fileName();
                        QString docFiles1 = docParhFile+"/configs/"+fileNames;
                        QFile file1(fileStr1);
                        if(file1.exists()){
                            if(file1.open(QIODevice::ReadOnly)){
                                QString infora1=  file1.readAll();
                                file1.close();
                                QFile wfile1(docFiles1);
                                if(wfile1.open(QIODevice::WriteOnly)){
                                    wfile1.write(infora1.toUtf8());
                                    wfile1.flush();
                                    wfile1.close();
                                    QFile::remove(fileStr1);

                                }
                            }
                        }

                    }
                }
            }



            /*
            QString docFiles1 = docParhFile+"/configs/configpins.json";
            QString docFiles2 = docParhFile+"/configs/levelpins.json";
            QString docFiles3 = docParhFile+"/configs/pinsinfors.json";
            QString docFiles4 = docParhFile+"/configs/versitons.ini";


            QString fileStr1 =docParhFile1+"configpins.json";
            QString fileStr2 =docParhFile1+"levelpins.json";
            QString fileStr3 =docParhFile1+"pinsinfors.json";
            QString fileStr4 =docParhFile1+"versitons.ini";
          //  qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"docFiles1=="<<docFiles1;
          //  qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"fileStr1=="<<fileStr1;
          //  qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"docFiles2=="<<docFiles2;
         //   qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"fileStr2=="<<fileStr2;
         //   qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"docFiles3=="<<docFiles3;
          //  qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"fileStr3=="<<fileStr3;
            QFile file1(fileStr1);
            if(file1.exists()){
                if(file1.open(QIODevice::ReadOnly)){
                    QString infora1=  file1.readAll();
                    file1.close();
                    QFile wfile1(docFiles1);
                    if(wfile1.open(QIODevice::WriteOnly)){
                        wfile1.write(infora1.toUtf8());
                        wfile1.flush();
                        wfile1.close();
                        QFile::remove(fileStr1);

                    }
                }
            }


            QFile file2(fileStr2);
            if(file2.exists()){
                if(file2.open(QIODevice::ReadOnly)){
                    QString infora2=  file2.readAll();
                    file2.close();
                    QFile wfile2(docFiles2);
                    if(wfile2.open(QIODevice::WriteOnly)){
                        wfile2.write(infora2.toUtf8());
                        wfile2.flush();
                        wfile2.close();
                        QFile::remove(fileStr2);

                    }
                }
            }
            QFile file3(fileStr3);
            if(file3.exists()){
                if(file3.open(QIODevice::ReadOnly)){
                    QString infora3=  file3.readAll();
                    file3.close();
                    QFile wfile3(docFiles3);
                    if(wfile3.open(QIODevice::WriteOnly)){
                        wfile3.write(infora3.toUtf8());
                        wfile3.flush();
                        wfile3.close();
                        QFile::remove(fileStr3);

                    }
                }
            }


            QFile file4(fileStr4);
            if(file4.exists()){
                if(file4.open(QIODevice::ReadOnly)){
                    QString infora4=  file4.readAll();
                    file4.close();
                    QFile wfile4(docFiles4);
                    if(wfile4.open(QIODevice::WriteOnly)){
                        wfile4.write(infora4.toUtf8());
                        wfile4.flush();
                        wfile4.close();
                        QFile::remove(fileStr4);

                    }
                }
            }
            */

        }
    }

    m_cleanTimerout = new QTimer(this);
    connect(m_cleanTimerout,SIGNAL( timeout() ),this,SLOT( onCleanoutTimerout() ));

    m_outTimer = new QTimer(this);
    connect(m_outTimer,SIGNAL( timeout() ) ,this ,SLOT( onOutTime() ) );

    m_mcuUpdateTimes = new QTimer(this);
    connect(m_mcuUpdateTimes,SIGNAL( timeout() ) ,this ,SLOT( onUpdateTime() ) );
    m_mcuTimer = new QTimer(this);
    connect(m_mcuTimer,SIGNAL( timeout() ) ,this ,SLOT( onMcuTimeout() ) );
    m_mcuCleanTimer = new QTimer(this);
    connect(m_mcuCleanTimer,SIGNAL( timeout() ) ,this ,SLOT( onMcuCleanTimeout() ) );

    m_testTimer = new QTimer(this);
    connect(m_testTimer,SIGNAL( timeout() ) ,this ,SLOT( onTestTimeout() ) );
    ConfigPin::gestance();
    PinsInfors::gestance();
    RegionalPins::gestance();
    CleanInfors::gestance();
    BlowairPins::gestance();

    DeviceConversion::gestance();
    DeviceStatus:: gestance()->setIsCleans(&m_isClean);
    DeviceStatus:: gestance()->setIsCleanPause(&m_isCleanPause);
    DeviceStatus:: gestance()->setIsOut( &m_isOut);
    MixDevice::gestance();
    ProtocolConvert::gestance();
    CommonBlowair::gestance();
    HandlDatabase::gestance();
    TimeTasks::gestance();
    UpdateProgrems::gestance()->writeFlile("");
    connect( TimeTasks::gestance() ,SIGNAL( sigTimeTasks(int  , int ) ),this,SLOT( onSigTimeTasks(int  , int ) ) );

    connect( ProtocolConvert::gestance() ,SIGNAL( sigFindLevel( ) ),this,SLOT( onSigFindLevel() ) );
    connect( ProtocolConvert::gestance() ,SIGNAL( sigSequentialDischarge(uchar ,QByteArray ) ),this,SLOT( onSigSequentialDischarge(uchar, QByteArray ) ) );
    connect( ProtocolConvert::gestance() ,SIGNAL( sigSameDischarge(uchar ,QByteArray ) ),this,SLOT( onSigSameDischarge(uchar , QByteArray ) ) );
    connect( ProtocolConvert::gestance() ,SIGNAL( sigSolenoidValve( uchar , QByteArray ) ),this,SLOT( onSigSolenoidValve( uchar ,QByteArray ) ) );
    connect( ProtocolConvert::gestance() ,SIGNAL( sigNotsupported( uchar , QByteArray ) ),this,SLOT( onSigNotsupported( uchar ,QByteArray ) ) );

    connect( DeviceConversion::gestance() ,SIGNAL( sigSequentialSuccess() ),this,SLOT( onSigSequentialSuccess() ) );
    connect( DeviceConversion::gestance() ,SIGNAL( sigSamelSuccess() ),this,SLOT( onSigSameSuccess() ) );
    connect( DeviceConversion::gestance() ,SIGNAL( sigCleanSuccess(int) ),this,SLOT( onSigCleanSuccess( int) ) );
    connect( DeviceConversion::gestance() ,SIGNAL( sigCloseCmds() ),this,SLOT( onSigCloseCmds( ) ) );
    connect( DeviceConversion::gestance() ,SIGNAL( sigHandlRunOutTime(int) ),this,SLOT( onHandlRunOutTime( int) ) );
    connect( DeviceConversion::gestance() ,SIGNAL( sigCleanRunTimeout(int) ),this,SLOT( onCleanRunTimeout( int) ) );



    connect( ProtocolConvert::gestance() ,SIGNAL( sigCleanIndex( uchar , int ) ),this,SLOT( onSigCleanIndex( uchar ,int ) ) );
    connect( MixDevice::gestance() ,SIGNAL( sigMixDeviceSuccess(  ) ),this,SLOT( onSigMixDeviceSuccess(  ) ) );

    connect( ProtocolConvert::gestance() ,SIGNAL( sigDevices( uchar  ) ),this,SLOT( onSigDevices( uchar  ) ) );
    connect( ProtocolConvert::gestance() ,SIGNAL( sigFindRuns( uchar  ) ),this,SLOT( onSigFindRuns( uchar  ) ) );

    connect( ProtocolConvert::gestance() ,SIGNAL( sigCloseValues( uchar,QByteArray  ) ),this,SLOT( onSigCloseValues( uchar  ,QByteArray) ) );


    connect( DeviceStatus::gestance() ,SIGNAL( sigElectric(int , int , int  ) ),this,SLOT( onSigElectric(int , int , int  ) ) );
    connect( DeviceStatus::gestance() ,SIGNAL( sigElectricHandl( ) ),this,SLOT( handlSigElectric() ) );
    connect( DeviceStatus::gestance() ,SIGNAL( sigWarterHandl( ) ),this,SLOT( onSigWarterHandl() ) );

    connect( DeviceStatus::gestance() ,SIGNAL( sigPinsInfor(QMap<int ,int> ) ),this,SLOT( onSigPinsInfor(QMap<int ,int> ) ) );
    QList<int>  lista = PinsInfors::gestance()->keyPins();
    for(int i = 0 ;i <lista.size() ;i++){
        int pina = lista.at(i);
        m_ledStatus.insert(pina,0);
        m_listPins.append(pina);
    }

    //    if(m_testTimer != NULL){
    //        m_testTimer->start(5000);
    //    }
}

ControlDevice::~ControlDevice()
{

}

ControlDevice *ControlDevice::gestance()
{
    if(m_controlDevice == NULL){
        m_controlDevice = new  ControlDevice();
    }
    return m_controlDevice;

}

void ControlDevice::setStartCompany(bool status)
{
#ifdef win32
    QString appName = QApplication::applicationName();//程序名称

    QString appPath = QApplication::applicationFilePath();// 程序路径

    appPath = appPath.replace("/","\\");

    QSettings *reg=new QSettings(
                "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                QSettings::NativeFormat);

    QString val = reg->value(appName).toString();// 如果此键不存在，则返回的是空字符串
    if(val != appPath) {
        if(status){
            reg->setValue(appName,appPath);// 如果移除的话，reg->remove(applicationName);
        }else {
            reg->remove(appName);
        }
    }else {
        if(!status){
            reg->remove(appName);
        }
    }

    reg->deleteLater();

#endif

}

bool ControlDevice::getStartCompany()
{
#ifdef win32
    QString appName = QApplication::applicationName();//程序名称

    QString appPath = QApplication::applicationFilePath();// 程序路径

    appPath = appPath.replace("/","\\");

    QSettings *reg=new QSettings(
                "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                QSettings::NativeFormat);

    QString val = reg->value(appName).toString();// 如果此键不存在，则返回的是空字符串
    reg->deleteLater();
    if(val != appPath) {
        return  false;
    }else {
        return  true;
    }
#endif
    return true;

}

void ControlDevice::handlStopOutTime()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_outTimer != NULL){
        m_outTimer->stop();
    }

}

void ControlDevice::handlRunOutTime(int times)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"times=="<<times;
#endif
    if(m_outTimer != NULL){
        m_outTimer->start(times);
    }
}

float ControlDevice::fourBytesToFloat(unsigned char byte1, unsigned char byte2, unsigned char byte3, unsigned char byte4)
{
    int bits=( byte1 << 24 ) + ( byte2 << 16 ) + ( byte3 << 8) + byte4;
    int s = ( bits >> 31 ) == 0 ? 1 : -1;
    int e = ( bits >> 23 ) & 0xff;
    int m = ( e == 0 ) ?
                ( bits & 0x7fffff ) << 1 :
                                       ( bits & 0x7fffff ) | 0x800000;
    return s * m * ( float ) pow( 2, e - 150 );
}

void ControlDevice::onSigPinsInfor(QMap<int, int> pinInfors)
{
    //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
    //    if(m_isClean || m_isOut){

    //        return;
    //    }
    //  qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pinInfors;
    if(true){
        return;
    }
    QMap<int, int>::iterator beginsa=  pinInfors.begin();
    QMap<int, QPair<int ,int> > pins;
    for(;beginsa != pinInfors.end();beginsa++){
        int pina =  beginsa.key();
        int valuea =  beginsa.value();
        //     qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        PinData * item =  PinsInfors::gestance()->getPin(pina);
        if(item == NULL){
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            continue;
        }
        //    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        int realPin = ConfigPin::gestance()->getRealPin(pina);
        if(realPin <= 0){
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            continue;
        }
        //    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        QString minHeight =  item->m_minHeight;
        QString maxHeight =  item->m_maxHeight;
        QString remindHeight =  item->m_remindHeight;
        QString remindLed =  item->m_remindLed;
        //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        int heaighta = maxHeight.toInt() - minHeight.toInt();
        int heaightb = valuea - minHeight.toInt();
        int heaightc = (heaighta - heaightb);
        //    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        if(heaightc <= remindLed.toInt() && m_ledStatus.value(pina) != 1){
            //  qDebug()<< metaObject()->className()<<__func__<<__LINE__;
            QPair<int ,int> paira(realPin,1);
            pins.insert(pina,paira);
            m_ledStatus.insert(pina,1);
            //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        }
        if(heaightc > remindLed.toInt() && m_ledStatus.value(pina) != 0){
            //  qDebug()<< metaObject()->className()<<__func__<<__LINE__;
            QPair<int ,int> paira(realPin,0);
            pins.insert(pina,paira);
            //    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
            m_ledStatus.insert(pina,0);
            //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        }
    }
    //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
    // pins
    if(pins.size() > 0 ){ //&& !m_isClean
        //  qDebug()<< metaObject()->className()<<__func__<<__LINE__;
        DeviceConversion::gestance()->handlLed(pins);
        //   qDebug()<< metaObject()->className()<<__func__<<__LINE__;
    }
    //  qDebug()<< metaObject()->className()<<__func__<<__LINE__;
}

void ControlDevice::onSigFindLevel()
{
#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);

#endif
    QString testInfors="";
    //DeviceStatus::gestance()->mBuffHeight;
    QMap<int ,int > minfors;
    QList<int>  lista = PinsInfors::gestance()->keyPins();
    for(int i = 0 ; i < lista.size();i++){
        int pins = lista.at(i);
        int pina = ConfigPin::gestance()->getRealPin(pins);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pina=="<<pina;

#endif
        PinData * itema = PinsInfors::gestance()->getPin(pins);
        if(itema == NULL){
            continue;
        }
        pina = pina-1;
        if(pina<= 0){
            pina =0;
        }
        int maxHeight =  itema->m_maxHeight.toInt();
        int minHeight =   itema->m_minHeight.toInt();
        int tempb = maxHeight - minHeight;
        if(itema->m_isValues <= 0){
            minfors.insert(pins,tempb);
            continue;
        }

        int heia= DeviceStatus::gestance()->mBuffHeight[pina ];
        heia = LevelPin::gestance()->getRealHeight(pins,heia);
        int tempa = maxHeight - heia;

#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"heia=="<<heia;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"maxHeight=="<<maxHeight;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"tempa=="<<tempa;
#endif
        if(heia ==0xFFFFFFFF){
            minfors.insert(pins,0xFFFFFFFF );
            testInfors+=QString("%1==%2;").arg(pins).arg(0xFFFFFFFF);
        }else{
            if((tempa) < 0 ){
                minfors.insert(pins,0);
                testInfors+=QString("%1==%2;").arg(pins).arg(0xFFFFFFFF);
            }else{
                minfors.insert(pins,tempa );
                testInfors+=QString("%1==%2;").arg(pins).arg(tempa);
            }
        }

    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"minfors=="<<minfors;
#endif
#ifdef DEBUG_LOG_INFOR
    QString printlogStrA=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)+" == "+testInfors  ;
    PrintLogb::gestance()->printInfor(printlogStrA);
#endif
    if(minfors.size() > 0){
        ProtocolConvert::gestance()->findLevel(minfors);
    }
}

void ControlDevice::onSigSequentialDischarge(uchar cmds,QByteArray infors)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors.toHex();
#endif
    if(m_isOut){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x01);
        return;
    }
    if(m_isClean){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x83);

        return;
    }
    m_isOut = true;
    m_cmds = cmds;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors.toHex();
#endif
    m_numberIndexs = 0;
    m_numberIndexs = (uchar)infors[1];
    m_numberIndexs= (m_numberIndexs << 8);
    m_numberIndexs += (uchar)infors[0];
    //0x07
    QMap<int , QPair< int ,int > > mapa;
    QMap<int , int> mapb;
    QList<int > lista;
    QList<int > listb;
    int spaces = 2;
    for(int i = spaces ; i < infors.size() ;i+= 5){
        int pins =(uchar) infors[i];
        int outs = (uchar)infors[i+4];
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
#endif
        outs= (outs << 8);
        outs += (uchar)infors[i+3];
        outs= (outs << 8);
        outs += (uchar)infors[i+2];
        outs= (outs << 8);
        outs += (uchar)infors[i+1];
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<outs;
#endif
        int pina = ConfigPin::gestance()->getRealPin(pins);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pina;
#endif
        PinData * itema = PinsInfors::gestance()->getPin(pins);
        if(itema == NULL){
            continue;
        }
        pina = pina-1;
        if(pina<= 0){
            pina =0;
        }
        int maxHeight =  itema->m_maxHeight.toInt();
        int minHeight =   itema->m_minHeight.toInt();
        int remindHeight =   itema->m_remindHeight.toInt();
        //        int heia= DeviceStatus::gestance()->mBuffHeight[pina ];
        //        heia = LevelPin::gestance()->getRealHeight(pins,heia);
        //        int heib= maxHeight - heia;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
#if 0
        if(remindHeight > heib){
            mapb.insert(pins,heib);
        }else{
            QPair< int ,int > pair(pina + 1,outs);
            lista.append(pina + 1);
            mapa.insert(pins,pair);
        }
#else
        QPair< int ,int > pair(pina + 1,outs);
        lista.append(pina + 1);
        mapa.insert(pins,pair);
        listb.append(pins);
#endif
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<mapb;
#endif
    if(mapb.size() > 0){
        m_isOut = false;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        ProtocolConvert::gestance()->lackMaterials(cmds,0x07);
    }else{
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);
        DeviceConversion::gestance()->handlSequentialDischarge(cmds,lista, mapa,listb );
    }
}

void ControlDevice::onSigSequentialSuccess()
{
    handlStopOutTime();
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_isOut){
        HandlDatabase::gestance()->setNumbersStautes(m_numberIndexs,2);
    }
    m_isOut = false;

    //  ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);
}

void ControlDevice::onSigSameDischarge(uchar cmds,QByteArray infors)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors.toHex();
#else
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") + QString("%1").arg(  QString(infors.toHex()) );
#endif
    if(m_isOut){
        if(cmds != 0x21){
            ProtocolConvert::gestance()->lackMaterials(cmds,0x01);
            return;
        }

    }
    if(m_isClean){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x83);
        return;
    }
    m_isOut = true;
    m_cmds = cmds;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors.toHex();
#endif
    if(cmds == 0x21){
        QMap<int , QPair< int ,int > > mapa;
        QMap<int , int> mapb;
        QList<int > lista;
        QList<int > listb;
        for(int i = 0 ; i < infors.size() ;i+= 2){
            int pins = infors[i];
            int outs = infors[i+1];
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"outs=="<<outs;
#endif
            if(outs==0){
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"outs=="<<outs;
#endif
                ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);

                DeviceConversion::gestance()->handlCloseCmd();
                return;
            }else{
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"outs=="<<outs;
#endif
                int outsb = 5 * 1000;
                int electricalMacha = HandlDatabase::gestance()->getElectricalMach(pins);
                int pipelinea = HandlDatabase::gestance()->getPipeline(pins);
                int relaysa = HandlDatabase::gestance()->getRelays(pins);
                electricalMacha+= outsb;
                pipelinea+= outsb;
                relaysa+= outsb;

                HandlDatabase::gestance()->setElectricalMach(pins,electricalMacha);
                HandlDatabase::gestance()->setPipeline(pins,pipelinea);
                HandlDatabase::gestance()->setRelays(pins,relaysa);

            }
            outs= 10 * 1000;
            int pina = ConfigPin::gestance()->getRealPin(pins);
            PinData * itema = PinsInfors::gestance()->getPin(pins);
            if(itema == NULL){
                continue;
            }
            pina = pina-1;
            if(pina<= 0){
                pina =0;
            }
            int maxHeight =  itema->m_maxHeight.toInt();
            int minHeight =   itema->m_minHeight.toInt();
            int remindHeight =   itema->m_remindHeight.toInt();
            int heia= DeviceStatus::gestance()->mBuffHeight[pina ];
            //  heia = LevelPin::gestance()->getRealHeight(pins,heia);
            int heib= maxHeight - heia;
#if 0
            if(remindHeight > heib){
                mapb.insert(pins,heib);
            }else{
                QPair< int ,int > pair(pina,outs);
                lista.append(pina);
                mapa.insert(pins,pair);
            }
#else
            QPair< int ,int > pair(pina + 1,outs);
            lista.append(pina + 1);
            mapa.insert(pins,pair);
            listb.append(pins);
#endif
        }

        if(mapb.size() > 0){
            m_isOut = false;

            ProtocolConvert::gestance()->lackMaterials(m_cmds,0x07);
        }else{
            ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);
            DeviceConversion::gestance()->handlSameDischarge(m_cmds,lista, mapa ,listb);
        }
    }else{
        QMap<int , QPair< int ,int > > mapa;
        QMap<int , int> mapb;
        QList<int > lista;
        QList<int > listb;
        m_numberIndexs = 0;
        m_numberIndexs = (uchar)infors[1];
        m_numberIndexs= (m_numberIndexs << 8);
        m_numberIndexs += (uchar)infors[0];
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_numberIndexs;
#endif
        int spaces = 2;
        for(int i = spaces ; i < infors.size()   ;i+= 5){
            int pins = (uchar)infors[i ];
            int outs = (uchar)infors[i+4];
            outs= (outs << 8);
            outs += (uchar)infors[i+3];
            outs= (outs << 8);
            outs += (uchar)infors[i+2];
            outs= (outs << 8);
            outs +=(uchar) infors[i+1];
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<outs;
#endif
            int electricalMacha = HandlDatabase::gestance()->getElectricalMach(pins);
            int pipelinea = HandlDatabase::gestance()->getPipeline(pins);
            int relaysa = HandlDatabase::gestance()->getRelays(pins);
            electricalMacha+= outs;
            pipelinea+= outs;
            relaysa+= outs;
            HandlDatabase::gestance()->setElectricalMach(pins,electricalMacha);
            HandlDatabase::gestance()->setPipeline(pins,pipelinea);
            HandlDatabase::gestance()->setRelays(pins,relaysa);

            int pina = ConfigPin::gestance()->getRealPin(pins);
            PinData * itema = PinsInfors::gestance()->getPin(pins);
            if(itema == NULL){
                continue;
            }
            pina = pina-1;
            if(pina<= 0){
                pina =0;
            }
            int maxHeight =  itema->m_maxHeight.toInt();
            int minHeight =   itema->m_minHeight.toInt();
            int remindHeight =   itema->m_remindHeight.toInt();
            int heia= DeviceStatus::gestance()->mBuffHeight[pina ];
            //  heia = LevelPin::gestance()->getRealHeight(pins,heia);
            int heib= maxHeight - heia;
#if 0
            if(remindHeight > heib){
                mapb.insert(pins,heib);
            }else{
                QPair< int ,int > pair(pina + 1,outs);
                lista.append(pina + 1);
                mapa.insert(pins,pair);
            }
#else
            QPair< int ,int > pair(pina + 1,outs);
            lista.append(pina + 1);
            mapa.insert(pins,pair);
            listb.append(pins);
#endif

        }

        if(mapb.size() > 0){
            m_isOut = false;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<mapb;
#endif
            ProtocolConvert::gestance()->lackMaterials(m_cmds,0x07);
        }else{
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"mapa =="<<mapa;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lista =="<<lista;
#endif
            ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);

            DeviceConversion::gestance()->handlSameDischarge(m_cmds,lista, mapa,listb );
        }
    }

}

void ControlDevice::onSigSameSuccess()
{
    handlStopOutTime();
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)     ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    if(m_isOut){
        HandlDatabase::gestance()->setNumbersStautes(m_numberIndexs,2);
    }
    m_isOut = false;

    //  ProtocolConvert::gestance()->lackMaterials(m_cmds,0x00);
}

void ControlDevice::onSigCleanIndex(uchar cmds,int indexs)
{
    if(m_isClean || m_isOut){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x83);

        return;
    }
    m_clenErrors.clear();
    m_isClean = true;
    m_isCleanPiPles= false;
    m_isCleanStops = false;
    m_cmds = cmds;
    m_cleanIndexs=indexs;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_cleanIndexs=="<<m_cleanIndexs;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"indexs=="<<indexs;
#endif
#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") +QString("%1").arg( cmds)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    QList<int>  lista = PinsInfors::gestance()->keyPins();
    for(int i = 0 ;i <lista.size() ;i++){
        m_ledStatus.insert(lista.at(i),0);
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"cmds=="<<cmds;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"indexs=="<<indexs;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_isClean=="<<m_isClean;
#endif
    ProtocolConvert::gestance()->cleanCmdEnd(cmds,0x00);
    DeviceConversion::gestance()->setCleanIndex( indexs);
}

void ControlDevice::onSigCleanSuccess(int errors)
{
    if(m_cleanTimerout != NULL){
        m_cleanTimerout->stop();
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"errors=="<<errors;
#endif
#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") +QString("%1").arg( errors)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif

    m_isCleanPause= false;
    m_isClean = false;
    m_isCleanPiPles= false;
    m_clenErrors.clear();

    // ProtocolConvert::gestance()->cleanCmdEnd(m_cmds,errors);
}

void ControlDevice::onSigSolenoidValve(uchar cmds, QByteArray infors)
{
    //  m_cmds = cmds;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" infors =="<<infors.toHex();
#endif
#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") + infors.toHex();
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    if(cmds == (uint8_t) 0x83){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors.toHex();
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<BlowairPins::gestance()->m_pinMap;
#endif
        for(int i = 0; i< infors.size() ; i+=2){
            int pina = (uchar) infors.at(i);
            int pins = BlowairPins::gestance()->getRealPin(pina);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pina=="<<pina;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
#endif
            if(pins <= 0){
                continue;
            }
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
#endif
            int status =  (uchar) infors.at(i+1);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<status;
#endif
            if(status){
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
#endif
                int timesa1 = QDateTime::currentSecsSinceEpoch();
                m_listTimeTasksMap.insert(pins,timesa1);
                m_listTimeTasks.insert(pins);
                TimeTasks::gestance()->runIndexs(pins);
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            }else{
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
#endif
                int timesa1 = QDateTime::currentSecsSinceEpoch();
                int timesa2 = m_listTimeTasksMap.value(pins);
                int timesa3 = timesa1 - timesa2;
                int timesa4 = HandlDatabase::gestance()->getPumps(pina);
                int timesa5 = timesa3+ timesa4;
                m_listTimeTasksMap.remove(pins);
                HandlDatabase::gestance()->setPumps(pina,timesa5);

                TimeTasks::gestance()->stopIndexs(pins);
                MixDevice::gestance()->closeCmdPins(pins);
                m_listTimeTasks.remove(pins);
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            }
        }
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasks;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasksMap;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasks;
#endif
        return;
    }
    if(cmds == (uint8_t) 0x88){

        bool isErrors = false;
        for(int i = 0; i< infors.size() ; i+=9){
            uint pins = infors.at(i);
            uint timesa = (uchar)infors.at(i+4);
            timesa=(timesa<<8);
            timesa +=  (uchar)infors.at(i+3);
            timesa=(timesa<<8);
            timesa += (uchar) infors.at(i+2);
            timesa=(timesa<<8);
            timesa +=  (uchar)infors.at(i+1);
            uint timesb = (uchar) infors.at(i+4 + 4);
            timesb=(timesb<<8);
            timesb +=  (uchar)infors.at(i+3 + 4);
            timesb=(timesb<<8);
            timesb += (uchar) infors.at(i+2+ 4);
            timesb=(timesb<<8);
            timesb +=  (uchar)infors.at(i+1+ 4);

#ifdef DEBUG_INFOR
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"timesa=="<<timesa;
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"timesb=="<<timesb;
#endif
            if(timesa <= 3000 ||timesb <= 3000 ){
                isErrors = true;
                break;
            }
        }
        if(isErrors){
            ProtocolConvert::gestance()->lackMaterials(cmds,0x05);
            return;
        }
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
        for(int i = 0; i< infors.size() ; i+=9){
            uint pins = infors.at(i);
            uint timesa = (uchar)infors.at(i+4);
            timesa=(timesa<<8);
            timesa +=  (uchar)infors.at(i+3);
            timesa=(timesa<<8);
            timesa += (uchar) infors.at(i+2);
            timesa=(timesa<<8);
            timesa +=  (uchar)infors.at(i+1);
            uint timesb = (uchar) infors.at(i+4 + 4);
            timesb=(timesb<<8);
            timesb +=  (uchar)infors.at(i+3 + 4);
            timesb=(timesb<<8);
            timesb += (uchar) infors.at(i+2+ 4);
            timesb=(timesb<<8);
            timesb +=  (uchar)infors.at(i+1+ 4);
#ifdef DEBUG_INFOR
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"timesa=="<<timesa;
            qDebug()<<typeid(this).name()<<__func__<<__LINE__<<"timesb=="<<timesb;
#endif
            CommonBlowair::gestance()->insertData(pins,timesa,timesb);
        }
        CommonBlowair::gestance()->saveRealBlowair();
        TimeTasks::gestance()->updateTimeTasks();
        return;
    }
    //  MixDevice::gestance()->handlCmdPins(infors);
    if(cmds == (uint8_t) 0x2C){
        int pins = 0;
        int outs = 0;
        QMap<int ,int > mapas;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" infors.size()  =="<<infors.size() ;

#endif
        for(int i = 0 ; i < infors.size() ;i+= 2){
            pins = (uchar)infors[i ];
            outs = (uchar)infors[i+1];
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" outs =="<<outs;
#endif
            if(pins >=0 && pins<=2 ){
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<<pins;

#endif
                mapas.insert(pins+1,outs);
            }


        }
        //        if(pins==0){
        //            DeviceConversion::gestance() ->handlReginLed("A",outs);
        //        }
        //        if(pins==1){
        //            DeviceConversion::gestance() ->handlReginLed("B",outs);
        //        }
        //        if(pins==2){
        //            DeviceConversion::gestance() ->handlReginLed("C",outs);
        //        }

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
        DeviceConversion::gestance() ->handlCmdLed(mapas);
        return;
    }
}

void ControlDevice::onSigMixDeviceSuccess()
{
    ProtocolConvert::gestance()->handlMixDeviceSuccess();
}

void ControlDevice::onSigNotsupported(uchar cmds, QByteArray infors)
{
    ProtocolConvert::gestance()->handlNotsupported(cmds,0x86);
}

void ControlDevice::onSigDevices(uchar cmds)
{

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ")  +QString("%1").arg( cmds)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif

    //mcu升级
    if(cmds == (uint8_t)0xA2){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        if(m_mcuUpdateTimes != NULL){
            m_mcuUpdateTimes->start(30);
        }

        return;
    }
    if(cmds == (uint8_t)0x54){
        char cha=0x00;
        QByteArray ba = QByteArray((char*)&cha,1);
        if(FridgeDevice::gestance()->getFridgeRuns()){

            cha=0x00;
            cha=FridgeDevice::gestance()->getDeviceStatus();


            for(int i =0 ;i<= 7 ;i++){
                if(i == 3){
                    uint8_t numSa = (uint8_t) DeviceStatus::gestance()->mBuffStatus.value(33-1,0) ;
                    uint8_t numSb =  (uint8_t)DeviceStatus::gestance()->mBuffStatus.value(34-1,0) ;
                    if(  numSa == 1 ||  numSb== 1){
                        cha=0x11;
                    }
                }else{
                    int sa =0;
                    switch (i ) {
                    case 0: //柜温探头故障
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x72;
                        }
                        break;
                    case 1://除霜探头故障
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x73;
                        }
                        break;

                    case 2://冷凝探头故障
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x14;
                        }
                        break;
                    case 3://门打开报警
//                        sa = FridgeDevice::gestance()->getCallPolice(i);
//                        if(sa ==1){
//                            cha=0x14;
//                        }
                        break;
                    case 4://柜温低温报警
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x12;
                        }
                        break;
                    case 5://柜温高温报警
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x13;
                        }
                        break;
                    case 6://冷凝高温报警
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x14;
                        }
                        break;
                    case 7://冷凝保护报警
                        sa = FridgeDevice::gestance()->getCallPolice(i);
                        if(sa ==1){
                            cha=0x15;
                        }
                        break;
                    default:
                        break;
                    }

                    if(cha != 0x00){
                        break;
                    }

                }

            }
            /*
         *
         * 柜温探头故障 0x0000 Bit 0：无报警 1：报警
         * 除霜探头故障 0x0001 Bit 0：无报警 1：报警
         * 冷凝探头故障 0x0002 Bit 0：无报警 1：报警
         * 门打开报警 0x0003 Bit 0：无报警 1：报警
         * 柜温低温报警 0x0004 Bit 0：无报警 1：报警
         * 柜温高温报警 0x0005 Bit 0：无报警 1：报警
         * 冷凝高温报警 0x0006 Bit 0：无报警 1：报警
         * 冷凝保护报警 0x0007 Bit 0：无报警 1：报警
         *
        */


        }else{
            cha=0x00;
        }

        QByteArray bb = QByteArray((char*)&cha,1);

        QByteArray devicea = ba+bb;
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }

    if(cmds == (uint8_t) 0xA4){
        char cha=0x00;
        QByteArray ba = QByteArray((char*)&cha,1);
        QString devices = QString("%1").arg( QDateTime::currentDateTime().toTime_t());
        QByteArray devicea= ba+devices.toUtf8();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"devices=="<<devices.toUtf8().toHex();
#endif
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }
    if(cmds == (uint8_t) 0x01){
        char cha=0x00;
        QByteArray ba = QByteArray((char*)&cha,1);
        QString devices = ConfigFile::gestance() ->getDevices();
        QByteArray devicea= ba+devices.toUtf8();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"devices=="<<devices.toUtf8().toHex();
#endif
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }
    if(cmds == (uint8_t) 0x02){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        QString devices = ConfigFile::gestance() ->getSns();
        QByteArray devicea= ba+devices.toUtf8();
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }
    if(cmds == (uint8_t) 0x03){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        QString devices = ConfigFile::gestance() ->getVersions();
        uint16_t lensa = (uint16_t)devices.toInt();
        char cgsa = (char)(lensa);
        char cgsb = (char)(lensa >> 8);
        QByteArray itemba(&cgsa,1);
        QByteArray itembb(&cgsb,1);
        QByteArray devicea= ba+cgsa+cgsb;
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }
    //mcu重启
    if(cmds == (uint8_t) 0xA0){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
        MixDevice::gestance()->openLanes(11,1);
        if(m_mcuTimer != NULL){
            m_mcuTimer->start(500);
        }
        return;
    }
    //    if(cmds == (uint8_t) 0xB0){
    //        char cha=0x00;
    //        QByteArray ba = QByteArray(&cha,1);
    //        QString devices = ConfigFile::gestance() ->getVersions();
    //        devices= ba+devices;
    //        ProtocolConvert::gestance()->handlDevices(cmds,devices);
    //        return;
    //    }

    if(cmds == (uint8_t) 0x11){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        QByteArray devices = "";
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"devices=="<<devices.toHex();
#endif
        qint64 timesa = QDateTime::currentMSecsSinceEpoch();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"timea=="<<timesa;
#endif
        for(int i =0 ;i < m_listPins.size() ;i++){
            int itema = m_listPins.at(i);
            int realPin = ConfigPin::gestance()->getRealPin(itema);
            int values = 0;
            values = DeviceStatus::gestance()->mBuffCricle.value(realPin -1,values);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"itema=="<<itema;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"realPin=="<<realPin;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"values=="<<values;
#endif
            char cgas = 0;
            if(values  > 0){
                cgas = 1;
            }
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"cgas=="<<cgas;
#endif
            QByteArray itemb(&cgas,1);
            devices+=itemb;
        }
        qint64 timesb = QDateTime::currentMSecsSinceEpoch();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"timesb=="<<timesb;
#endif
        qint64 timesc = timesb - timesa;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"timesc=="<<timesc;
#endif
        char cgbs = (char)devices.length();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"cgbs=="<<cgbs;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"devices=="<<devices.toHex();
#endif
        QByteArray itemc(&cgbs,1);

        QByteArray devicea= ba+itemc+devices;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"devicea=="<<devicea.toHex();
#endif
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }
    //查询点击运行状态
    if(cmds == (uint8_t) 0x12){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        QByteArray devices = "";

        for(int i =0 ;i < m_listPins.size() ;i++){
            int itema = m_listPins.at(i);
            int realPin = ConfigPin::gestance()->getRealPin(itema);
            int values = 0;
            // values = DeviceStatus::gestance()->mBuffCricle.value(realPin -1,values);
            char cgas = 0x00;
            QByteArray itemb(&cgas,1);
            devices+=itemb;
        }
        char cgbs = (char)devices.length();
        QByteArray itemc(&cgbs,1);


        QByteArray devicea= ba+itemc+devices;
        ProtocolConvert::gestance()->handlDevices(cmds,devicea);
        return;
    }

    //恢复出厂设置
    if( cmds == (uint8_t)0xA3){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
        CommonBlowair::gestance()->restoreData();
        TimeTasks::gestance()->updateTimeTasks();
        return;
    }
    //泵全部停止
    if(cmds == (uint8_t) 0x86){

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        QList<int> lista =  m_listTimeTasksMap.keys();
        for(int i = 0; i< lista.size() ; i++){
            int pina = (uchar) lista.at(i);
            int pins = pina;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pina=="<<pina;

            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
#endif
            int timesa1 = QDateTime::currentSecsSinceEpoch();
            int timesa2 = m_listTimeTasksMap.value(pins);
            int timesa3 = timesa1 - timesa2;
            int timesa4 = HandlDatabase::gestance()->getPumps(pina);
            int timesa5 = timesa3+ timesa4;
            m_listTimeTasksMap.remove(pins);
            HandlDatabase::gestance()->setPumps(pina,timesa5);

            TimeTasks::gestance()->stopIndexs(pins);
            MixDevice::gestance()->closeCmdPins(pins);
            m_listTimeTasks.remove(pins);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        }
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasks;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasksMap;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_listTimeTasks;
#endif
        return;

        //        CommonBlowair::gestance()->restoreData();
        //        TimeTasks::gestance()->updateTimeTasks();
        return;
    }
    //电机的时间清零
    if(cmds == (uint8_t) 0xC1){
        QList<int> keyPins = PinsInfors::gestance()->keyPins();


        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            HandlDatabase::gestance()->setElectricalMach(pins,0);


        }

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        return;
    }
    //管道的时间清零
    if(cmds == (uint8_t) 0xC3){
        QList<int> keyPins = PinsInfors::gestance()->keyPins();


        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            HandlDatabase::gestance()->setPipeline(pins,0);


        }

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        return;
    }

    //泵的时间清零
    if(cmds == (uint8_t) 0xC5){
        QList<int> keyPins = PinsInfors::gestance()->keyPins();


        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            HandlDatabase::gestance()->setPumps(pins,0);


        }

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        return;
    }

    //电磁阀的时间清零
    if(cmds == (uint8_t) 0xC7){
        QList<int> keyPins = PinsInfors::gestance()->keyPins();


        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            HandlDatabase::gestance()->setRelays(pins,0);


        }

        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        return;
    }
    //热水
    if( cmds == (uint8_t) 0x5D){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x01;
        QByteArray bb  = QByteArray(&cha,1);

        QByteArray bc;
        int redapsa = DeviceStatus::gestance()->mBuffrempute.value(1,0);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsa=="<<redapsa;
#endif
        float redapsb = (float)redapsa;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsb=="<<redapsb;
#endif
        char chInfa[4];
        memcpy(chInfa,(char*)&redapsb,4);
        char chInfb[4];
        memcpy(chInfb,(char*)&redapsb,4);
        QByteArray bca(chInfa,4);
        QByteArray bcb(chInfb,4);
        bc.append(bca);
        bc.append(bcb);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"bc=="<<bc.toHex();
#endif
        QByteArray infors=ba+bb+bc;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);

        return;
    }
    //冰水
    if( cmds == (uint8_t) 0x6C){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x01;
        QByteArray bb  = QByteArray(&cha,1);
        cha=0x01;
        QByteArray bb1  = QByteArray(&cha,1);

        QByteArray bc;
        int redapsa = DeviceStatus::gestance()->mBuffrempute.value(0,0);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsa=="<<redapsa;
#endif
        float redapsb = (float)redapsa;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsb=="<<redapsb;
#endif
        char chInfa[4];
        memcpy(chInfa,(char*)&redapsb,4);
        char chInfb[4];
        memcpy(chInfb,(char*)&redapsb,4);
        QByteArray bca(chInfa,4);
        QByteArray bcb(chInfb,4);
        bc.append(bca);
        bc.append(bcb);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"bc=="<<bc.toHex();
#endif
        QByteArray infors=ba+bb+bb1+bc;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);

        return;
    }

    //冰箱温度
    if( cmds == (uint8_t) 0x53){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);

        if(FridgeDevice::gestance()->getFridgeRuns()){
            cha=FridgeDevice::gestance()->getDeviceStatus();
        }else{
            cha=0x01;
        }
        QByteArray bb  = QByteArray(&cha,1);

        QByteArray bc;
        int redapsa = 0;

        if(FridgeDevice::gestance()->getFridgeRuns()){
            redapsa = FridgeDevice::gestance()->getCabinetTemp();

        }else{
            redapsa = DeviceStatus::gestance()->mBuffrempute.value(2,0);

        }

#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsa=="<<redapsa;
#endif
        float redapsb = (float)redapsa;
        float redapsc =0.0;
        if(FridgeDevice::gestance()->getFridgeRuns()){
            redapsc = FridgeDevice::gestance()->getMaxsTemp();

        }else{
            redapsc = (float)redapsa;
        }

#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"redapsb=="<<redapsb;
#endif
        char chInfa[4];
        memcpy(chInfa,(char*)&redapsc,4);
        char chInfb[4];
        memcpy(chInfb,(char*)&redapsb,4);
        QByteArray bca(chInfa,4);
        QByteArray bcb(chInfb,4);
        bc.append(bca);
        bc.append(bcb);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"bc=="<<bc.toHex();
#endif
        QByteArray infors=ba+bb+bc;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);


    }



}

void ControlDevice::onSigFindRuns(uchar cmds)
{

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ")  +QString("%1").arg( cmds)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"cmds=="<<cmds;
#endif
    if(cmds == (uint8_t) 0x10){
        uint8_t numSa = (uint8_t) DeviceStatus::gestance()->mBuffStatus.value(33-1,0) ;
        uint8_t numSb =  (uint8_t)DeviceStatus::gestance()->mBuffStatus.value(34-1,0) ;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_isOut=="<<m_isOut;
#endif
        if(m_isOut){
            char cha=0x00;
            QByteArray ba = QByteArray(&cha,1);
            cha=0x01;
            QByteArray bb = QByteArray(&cha,1);

            QByteArray infors=ba+bb;
            if(  numSa == 1 ||  numSb== 1){
                cha=0x01; //0x80
                QByteArray bc = QByteArray(&cha,1);
                infors+=bc;
            }else{
                cha=0x00; //0x80
                QByteArray bc = QByteArray(&cha,1);
                infors+=bc;
            }

            ProtocolConvert::gestance()->handlDevices(cmds,infors);
            return;
        }
        if(m_isClean){
            if(m_isCleanPause){
                char cha=0x00;
                QByteArray ba = QByteArray(&cha,1);
                cha=0x04;
                QByteArray bb = QByteArray(&cha,1);

                QByteArray infors=ba+bb;
                if(  numSa == 1 ||  numSb== 1){
                    cha=0x01; //0x80
                    QByteArray bc = QByteArray(&cha,1);
                    infors+=bc;
                }else{
                    cha=0x00; //0x80
                    QByteArray bc = QByteArray(&cha,1);
                    infors+=bc;
                }
                ProtocolConvert::gestance()->handlDevices(cmds,infors);
                return;
            }else{
                char cha=0x00;
                QByteArray ba = QByteArray(&cha,1);
                cha=0x03;
                QByteArray bb = QByteArray(&cha,1);

                QByteArray infors=ba+bb;
                if(  numSa == 1 ||  numSb== 1){
                    cha=0x01; //0x80
                    QByteArray bc = QByteArray(&cha,1);
                    infors+=bc;
                }else{
                    cha=0x00; //0x80
                    QByteArray bc = QByteArray(&cha,1);
                    infors+=bc;
                }
                ProtocolConvert::gestance()->handlDevices(cmds,infors);
                return;
            }


        }

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00; //0x80
        QByteArray bb = QByteArray(&cha,1);

        QByteArray infors=ba+bb;

        if(  numSa == 1 ||  numSb== 1){
            cha=0x01; //0x80
            QByteArray bc = QByteArray(&cha,1);
            infors+=bc;
        }else{
            cha=0x00; //0x80
            QByteArray bc = QByteArray(&cha,1);
            infors+=bc;
        }


        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }


    //出料点击转速查询
    if(cmds == (uint8_t) 0x2A){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;//= QByteArray(&cha,1);

        QList<int> lista =  PinsInfors::gestance()->keyPins();
        int lena =lista.size();
        bb.append((char)lena);
        for(int i =0 ; i < lista.size() ;i++){
            int pina = lista.at(i);
            PinData * pinData= PinsInfors::gestance()->getPin(pina);
            if(pinData == NULL){
                continue;
            }
            int speeda =pinData->m_speed.toInt() ;
            bb.append((char)speeda);
            bb.append((char)(0x00));
            bb.append((char)(0x00));
            bb.append((char)(0x00));

        }

        QByteArray infors=ba+bb;
        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    //清洗脚本信息
    if(cmds == (uint8_t) 0x41){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;
        QList<int> lista =  CleanInfors::gestance()->keyPins();
        for(int i =0 ; i < lista.size() ;i++){
            int pina = lista.at(i);
            QList<QJsonObject> listb = CleanInfors::gestance()->getPin(pina);
            int versions = CleanInfors::gestance()->getVersion(pina);
            int stepa =listb.size();
            bb.append((char)pina);
            bb.append((char)(versions));
            bb.append((char)(versions >> 8));
            bb.append((char)(stepa));
            bb.append((char)(stepa >> 8));
        }
        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //清洗进度查询
    if(cmds == (uint8_t) 0x47){
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_isClean=="<<m_isClean;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_cleanIndexs=="<<m_cleanIndexs;
#endif
        if(m_isClean){
            if(m_isCleanPause){
                if(m_clenErrors.size() >0){
                    bb.append((char) 0x72);
                }else{
                    if(m_isCleanPiPles){
                        bb.append((char) 0x75);

                    }else{
                        bb.append((char) 0x02);

                    }

                }

            }else{
                bb.append((char) 0x00);
            }

        }else{
            if(m_isCleanStops){
                bb.append((char) 0x05);
            }else{
                if( DeviceConversion::gestance()->getCleanError() > 0){
                    if(DeviceConversion::gestance()->getCleanError() == 17){
                        bb.append((char) 0x71);
                    }else{
                        bb.append((char) 0x80);
                    }

                }else{
                    bb.append((char) 0x01);
                }

            }


        }
        bb.append((char)m_cleanIndexs);

        QList<QJsonObject> listb = CleanInfors::gestance()->getPin(m_cleanIndexs);
        int stepa =listb.size();
        bb.append((char)(stepa));
        bb.append((char)(stepa >> 8));
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"listb=="<<listb;
#endif

        int selectCleanIndexs = DeviceConversion::gestance()->getSelectCleanIndexs();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"selectCleanIndexs=="<<selectCleanIndexs;
#endif

        bb.append((char)(selectCleanIndexs));
        bb.append((char)(selectCleanIndexs >> 8));


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }


    //查询搅拌泵的状态
    if(cmds == (uint8_t) 0x84){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = CommonBlowair::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int pinas =BlowairPins::gestance()->getRealPin(pins);
            if(m_listTimeTasks.contains(pinas)){
                bb.append((char)1);
            }else{
                bb.append((char)0);
            }
        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //查询搅拌故障
    if(cmds == (uint8_t) 0x85){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = CommonBlowair::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            bb.append((char)0);

        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //查询搅拌泵的运行时间
    if(cmds == (uint8_t) 0x87){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = CommonBlowair::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            QPair<int, int> pairs = CommonBlowair::gestance()-> getPinData( pins);
            int lena =  pairs.first;
            int lenb =  pairs.second;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lena=="<<lena;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lenb=="<<lenb;
#endif
            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));

            bb.append((char)(lenb));
            bb.append((char)(lenb >> 8));
            bb.append((char)(lenb >> 16));
            bb.append((char)(lenb >> 24));

        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    //液位传感器
#if 0
    if(cmds == (uint8_t) 0x8E){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int heightsa = DeviceStatus::gestance()->mBuffHeight.value(realPin - 1);
            PinData * pinData = PinsInfors::gestance()-> getPin( pins);
            int minHeight = pinData->m_minHeight.toInt();
            int maxHeight = pinData->m_maxHeight.toInt();
            int heightsb = maxHeight - heightsa;
            //            int lena =  pairs.first;
            //            int lenb =  pairs.second;

            bb.append((char)(heightsb));
            bb.append((char)(heightsb >> 8));
            bb.append((char)(heightsb >> 16));
            bb.append((char)(heightsb >> 24));

            //            bb.append((char)(lenb));
            //            bb.append((char)(lenb >> 8));
            //            bb.append((char)(lenb >> 16));
            //            bb.append((char)(lenb >> 24));

        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
#endif
    //压力
    if(cmds == (uint8_t) 0x9A){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int heightsa = DeviceStatus::gestance()->mBuffHeight.value(realPin - 1);
            //     heightsa = LevelPin::gestance()->getRealHeight(pins,heightsa);
            //            int lena =  pairs.first;
            //            int lenb =  pairs.second;

            bb.append((char)(0x00));

            //            bb.append((char)(lenb));
            //            bb.append((char)(lenb >> 8));
            //            bb.append((char)(lenb >> 16));
            //            bb.append((char)(lenb >> 24));

        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //点击运行状态
    if(cmds == (uint8_t) 0xB0){
        QByteArray infors;

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        infors.append(ba);

        //设备运行状态
        cha=0x00;
        QByteArray bb = QByteArray(&cha,1);
        infors.append(bb);
        int lena = 1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));

        if(m_isOut || m_isClean){
            if(m_isOut){

                cha=0x01;
                QByteArray bb1 = QByteArray(&cha,1);
                infors.append(bb1);

            }
            if(m_isClean){

                if(m_isCleanPause){
                    bb.append((char) 0x04);
                }else{
                    bb.append((char) 0x03);
                }
                infors.append(bb);


            }

        }else {
            cha=0x00;
            QByteArray bb1 = QByteArray(&cha,1);

            infors.append(bb1);

        }



        //电机运行状态
        cha=0x01;
        bb = QByteArray(&cha,1);
        infors.append(bb);
        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        lena = keyPins.size();
        lena+=1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));
        lena-=1;
        infors.append((char)lena);
        for(int i =0 ;i < keyPins.size() ;i++){
            int itema = keyPins.at(i);
            int realPin = ConfigPin::gestance()->getRealPin(itema);
            int values = 0;
            values = DeviceStatus::gestance()->mBuffCricle.value(realPin -1,values);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"itema=="<<itema;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"realPin=="<<realPin;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"values=="<<values;
#endif
            char cgas = 0;
            if(values  > 0){
                cgas = 1;
            }
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"cgas=="<<cgas;
#endif
            QByteArray itemb(&cgas,1);
            infors.append(itemb);
        }

        //电机运行状态
        cha=0x07;
        bb = QByteArray(&cha,1);
        infors.append(bb);

        keyPins = CommonBlowair::gestance()->keyPins();
        lena = keyPins.size();
        lena+=1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));
        lena-=1;
        infors.append((char)lena);

        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int pinas =BlowairPins::gestance()->getRealPin(pins);
            if(m_listTimeTasks.contains(pinas)){
                infors.append((char)1);
            }else{
                infors.append((char)0);
            }
        }


        //液位
        cha=0x0A;
        bb = QByteArray(&cha,1);
        infors.append(bb);

        QList<int> lista = PinsInfors::gestance()->keyPins();
        lena = lista.size() * 4+1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));
        lena=lena - 1;
        lena  =lena / 4;
        infors.append((char)lena);
        for(int i = 0 ; i < lista.size();i++){
            int pins = lista.at(i);
            int pina = ConfigPin::gestance()->getRealPin(pins);
            PinData * itema = PinsInfors::gestance()->getPin(pins);
            if(itema == NULL){
                continue;
            }
            pina = pina-1;
            if(pina<= 0){
                pina =0;
            }
            int maxHeight =  itema->m_maxHeight.toInt();
            int minHeight =   itema->m_minHeight.toInt();
            int heia= DeviceStatus::gestance()->mBuffHeight[pina ];
            heia = LevelPin::gestance()->getRealHeight(pins,heia);
            if(heia ==0xFFFFFFFF){
                infors.append((char)0xFF);
                infors.append((char)0xFF);
                infors.append((char)0xFF);
                infors.append((char)0xFF);

            }else{
                if((maxHeight - heia) < 0 ){
                    infors.append((char)0xFF);
                    infors.append((char)0xFF);
                    infors.append((char)0xFF);
                    infors.append((char)0xFF);
                }else{
                    uint heib=maxHeight - heia;

                    infors.append((uchar)heib);
                    infors.append((uchar)(heib >> 8));
                    infors.append((uchar)(heib >> 16));
                    infors.append((uchar)(heib >> 24));
                }
            }

        }

        //速度
        cha=0x0B;
        bb = QByteArray(&cha,1);
        infors.append(bb);
        lista =  PinsInfors::gestance()->keyPins();
        lena =lista.size() * 4+1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));
        lena=lena - 1;
        lena  =lena / 4;
        infors.append((char)lena);
        for(int i =0 ; i < lista.size() ;i++){
            int pina = lista.at(i);
            PinData * pinData= PinsInfors::gestance()->getPin(pina);
            if(pinData == NULL){
                continue;
            }
            int speeda =pinData->m_speed.toInt() ;
            infors.append((char)speeda);
            infors.append((char)(0x00));
            infors.append((char)(0x00));
            infors.append((char)(0x00));

        }


        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    if(cmds == (uint8_t) 0xB1){
        QByteArray infors;
        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        infors.append(ba);
        cha=0x01;
        QByteArray bb= QByteArray(&cha,1);
        infors.append(bb);

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size() + 1;
        infors.append((char)lena);
        infors.append((char)(lena>>8));
        lena = lena - 1;
        infors.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int statusa = DeviceStatus::gestance()->mBuffCricle.value(realPin - 1);


            infors.append((char)(0x00));






        }




        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    //电机运行时间
    if(cmds == (uint8_t) 0xC0){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<HandlDatabase::gestance()->m_electricalMach;
#endif
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int statusa = DeviceStatus::gestance()->mBuffCricle.value(realPin - 1);
            PinData * pinData = PinsInfors::gestance()-> getPin( pins);
            int lena =  HandlDatabase::gestance()->getElectricalMach(pins)  / 60 / 1000;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lena=="<<lena;
#endif
            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    //管道运行时间
    if(cmds == (uint8_t) 0xC2){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int statusa = DeviceStatus::gestance()->mBuffCricle.value(realPin - 1);
            PinData * pinData = PinsInfors::gestance()-> getPin( pins);
            int lena =  HandlDatabase::gestance()->getPipeline(pins)  / 60 / 1000;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lena=="<<lena;
#endif
            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }

    //泵运行时间
    if(cmds == (uint8_t) 0xC4){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int statusa = DeviceStatus::gestance()->mBuffCricle.value(realPin - 1);
            PinData * pinData = PinsInfors::gestance()-> getPin( pins);
            int lena =  HandlDatabase::gestance()->getPumps(pins) / 60 / 1000;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lena=="<<lena;
#endif
            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }


    //电磁阀运行时间
    if(cmds == (uint8_t) 0xC6){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);
            int realPin =  ConfigPin::gestance()-> getRealPin( pins);
            int statusa = DeviceStatus::gestance()->mBuffCricle.value(realPin - 1);
            PinData * pinData = PinsInfors::gestance()-> getPin( pins);
            int lena =  HandlDatabase::gestance()->getRelays(pins) / 60 / 1000;
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lena=="<<lena;
#endif

            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //所有设备状态
    if(cmds == (uint8_t) 0xC8){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);
        cha=0x00;
        QByteArray bb ;

        QList<int> keyPins = PinsInfors::gestance()->keyPins();
        int lena = keyPins.size();
        bb.append((char)0x00);
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            int lena =  HandlDatabase::gestance()->getElectricalMach(pins);


            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }
        bb.append((char)0x01);
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            int lena =  HandlDatabase::gestance()->getPipeline(pins);


            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }
        bb.append((char)0x02);
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            int lena =  HandlDatabase::gestance()->getPumps(pins);


            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }

        bb.append((char)0x03);
        bb.append((char)lena);
        for(int i =0;i < keyPins.size() ;i++){
            int pins =  keyPins.at(i);

            int lena =  HandlDatabase::gestance()->getRelays(pins);


            bb.append((char)(lena));
            bb.append((char)(lena >> 8));
            bb.append((char)(lena >> 16));
            bb.append((char)(lena >> 24));



        }


        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;
    }
    //查询灯光
    if(cmds == (uint8_t) 0x2D){

        char cha=0x00;
        QByteArray ba = QByteArray(&cha,1);

        QByteArray bb ;
        cha=0x03;
        bb.append(QByteArray(&cha,1) ) ;

        cha=DeviceConversion::gestance() ->getRegnInforsLed(1);
        bb.append(QByteArray(&cha,1) ) ;

        cha=DeviceConversion::gestance() ->getRegnInforsLed(2);
        bb.append(QByteArray(&cha,1) ) ;

        cha=DeviceConversion::gestance() ->getRegnInforsLed(3);
        bb.append(QByteArray(&cha,1) ) ;
        QByteArray infors=ba+bb;

        ProtocolConvert::gestance()->handlDevices(cmds,infors);
        return;

    }
}

void ControlDevice::onSigCloseCmds()
{
    handlStopOutTime();

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ")   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    if(m_isOut){
        HandlDatabase::gestance()->setNumbersStautes(m_numberIndexs,2);
    }

    m_isCleanPause= false;
    m_isClean = false;
    m_isOut= false;
    m_isCleanPiPles= false;
    m_clenErrors.clear();
}

void ControlDevice::onSigCloseValues(uchar cmds, QByteArray infors)
{

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") +QString("%1").arg( cmds) + QString("%1").arg( QString(infors.toHex()) ) ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    if(cmds == (uint8_t) 0x28){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);
        int status =  (uchar)infors[0];
        if(status == 0){
            DeviceConversion::gestance()->handlCloseCmd();
        }
        //            if(m_isClean){
        //                m_isCleanStops = true;
        //            }
        //        }else if(status == 1){
        //            if(m_isClean){
        //                m_isCleanPause = true;
        //                DeviceConversion::gestance()->pauseProgram();
        //            }

        //        }else if(status == 2){
        //            if(m_isClean){
        //                m_isCleanPause = false;
        //                DeviceConversion::gestance()->goOnProgram();
        //            }
        //        }
        return;
    }

    if(cmds == (uint8_t) 0x48){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        int status = (uchar)infors[0];
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" status =="<<status;
#endif
        if(status == 0){
            DeviceConversion::gestance()->handlCloseCmd();
            if(m_isClean){
                m_isCleanStops = true;
                m_isCleanPiPles= false;
                m_clenErrors.clear();
            }
        }else if(status == 1){
            if(m_isClean){
                m_isCleanPause = true;
                m_isCleanPiPles= false;
                DeviceConversion::gestance()->pauseProgram();
            }

        }else if(status == 2){
            if(m_isClean){
                m_isCleanPause = false;
                m_isCleanPiPles= false;
                DeviceConversion::gestance()->goOnProgram();
            }
        }
        return;
    }

    if(cmds == (uint8_t) 0x29){
        int numberIndexs = 0;
        numberIndexs =(uchar) infors[1];
        numberIndexs= (numberIndexs << 8);
        numberIndexs += (uchar)infors[0];
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" numberIndexs =="<<numberIndexs;
#endif
        if(m_isOut == true){
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" numberIndexs =="<<numberIndexs;
#endif
            if(numberIndexs == m_numberIndexs){
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" numberIndexs =="<<numberIndexs;
#endif
                ProtocolConvert::gestance()->handlNotsupported(0x29,0x01,0x00);
                return;
            }
        }
        //        else{
        //            if(numberIndexs == m_numberIndexs){
        //#ifdef DEBUG_INFOR
        //                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" numberIndexs =="<<numberIndexs;
        //#endif
        //                ProtocolConvert::gestance()->handlNotsupported(0x29,0x01,0x00);
        //                return;
        //            }
        //        }
        int statusa = HandlDatabase::gestance()->getNumbersStautes(numberIndexs);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" statusa =="<<statusa;
#endif
        if(statusa < 0){
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" statusa =="<<statusa;
#endif
            ProtocolConvert::gestance()->handlNotsupported(0x29,0x06,0x00);
            return;
        }
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" statusa =="<<statusa;
#endif
        ProtocolConvert::gestance()->handlNotsupported(0x29,statusa,0x00);
        return;
    }

    //冰箱开启关闭
    if( cmds == (uint8_t) 0x51 ){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        int status =  (uchar)infors[0];
        if(status == 0){
            FridgeDevice::gestance()->closeDevice();

        }
        if(status == 1){

            FridgeDevice::gestance()->openDevice();
        }
        return;
    }
    // 冰箱温度设置
    if( cmds == (uint8_t) 0x52 ){
        ProtocolConvert::gestance()->lackMaterials(cmds,0x00);

        float temps = fourBytesToFloat( (uchar) infors[3], (uchar) infors[2],(uchar)infors[1],(uchar)infors[0]);
        int temp = (int)temps;
        int tempa = temps -10;
        int tempb= temps;
        FridgeDevice::gestance()->setTemperature(tempa,tempb);
        return;
    }
}

void ControlDevice::onSigTimeTasks(int pin, int statues)
{

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ") +QString("%1").arg( pin)  +QString("%1").arg(statues ) ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pin =="<<pin;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" statues =="<<statues;
#endif
    if(m_listTimeTasks.contains(pin)){

        if(statues == 1){
            MixDevice::gestance()->openCmdPins(pin);
            return;
        }
        if(statues == 0){
            MixDevice::gestance()->closeCmdPins(pin);
            return;
        }

    }
}

void ControlDevice::onMcuTimeout()
{
    MixDevice::gestance()->closeLanes(11,0);
    if(m_mcuTimer != NULL){
        m_mcuTimer->stop();
    }

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif


    qApp->exit(0);
}

void ControlDevice::onMcuCleanTimeout()
{
    if(m_mcuCleanTimer != NULL){
        m_mcuCleanTimer->stop();
    }
    MixDevice::gestance()->closeLanes(11,0);

}

void ControlDevice::onTestTimeout()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;

#endif
    if(m_testTimer != NULL){
        m_testTimer->stop();
    }
    UpdateProgrems::gestance()->receivePaths();
}

void ControlDevice::onSigElectric(int userPin, int realPin, int status)
{
    m_clenErrors.insert(userPin,status);

}

void ControlDevice::onSigWarterHandl()
{
    m_isCleanPiPles= true;
}

void ControlDevice::handlSigElectric()
{
    m_isCleanPause = true;
    DeviceConversion::gestance()->pauseProgram();
}

void ControlDevice::onUpdateTime()
{
    if(m_mcuUpdateTimes != NULL){
        m_mcuUpdateTimes->stop();
    }

#ifdef DEBUG_LOG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  + QString(" = ")   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;

#endif
    UpdateProgrems::gestance()->receivePaths();
}

void ControlDevice::onOutTime()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    handlStopOutTime();
    m_isOut = false;
    HandlDatabase::gestance()->setNumbersStautes(m_numberIndexs,7);
    m_numberIndexs=0;
}

void ControlDevice::onHandlRunOutTime(int times)
{
    handlRunOutTime(times);
}

void ControlDevice::onCleanoutTimerout()
{

    if(m_cleanTimerout != NULL){
        m_cleanTimerout->stop();
    }
    if(m_isClean && !m_isCleanStops ){
        if( !m_isCleanPause){
            m_isCleanPause= false;
            m_isClean = false;

            DeviceConversion::gestance()->setCleanError( 128);
            MixDevice::gestance()->openLanes(11,1);
            if(m_mcuCleanTimer != NULL){
                m_mcuCleanTimer->start(500);
            }
            return;
        }

    }

    if(m_cleanTimerout != NULL){
        m_cleanTimerout->start(m_cleanLens);
    }

}

void ControlDevice::onCleanRunTimeout(int times)
{
    if(m_cleanTimerout != NULL){
        m_cleanTimerout->stop();
    }

    m_cleanLens = times;
    if(m_cleanTimerout != NULL){
        m_cleanTimerout->start(m_cleanLens);
    }
}
